﻿using Mirror;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

class UAV : Weapon
{
    public enum UAVType
    {
        TYPE_FIGHT,
        TYPE_BOMB,
    }
    public int CurrentNumberOfUAV {get{return m_UAVSortileList.Count + m_LoadingCount; }}
    public UAVType m_UAVType = UAVType.TYPE_FIGHT;
    public float m_TrunVelocity = 90f;
    public float m_UAVHP = 100;
    public int m_LoadingCapacity = 2;
    public int m_LoadingCount=2;
    public int m_AmmoCapacity = 1;
    public float m_BornSpend = 300;
    public Ship m_Target;
    List<UAVFunction> m_UAVSortileList = new List<UAVFunction>();

    float m_OutTimeMax = 1.5f;
    float m_OutTime = 1.5f;
    public UAV()
    {
        m_IsCanShot = false;
    }

    public override FlyItem FireRequst(GameObject ship, WeaponSlot slot, Vector3 position, Vector3 direction)
    {
        if (ship)
            m_Ship = ship.GetComponent<Ship>();
        Slot = slot;
        if (m_LoadingCount <= 0)
        {
            //MessageShow.Instance.Messaage(Name + "-没有可使用的战机");
            return null;
        }
        if (m_OutTime < m_OutTimeMax)
            return null;
        if (!m_Ship || m_Ship.m_ShipStruct.Electric < m_BornSpend)
            return null;
        m_Ship.m_ShipStruct.Electric -= m_BornSpend;


        m_OutTime = 0;

        if(UtilsGame.IsOnline() && !NetworkServer.active)
        {
            PlayFireSound(ship.GetComponent<Ship>(), position);
            return null;
        }
        var item = base.Fire(ship, slot, position, direction);
        UAVFunction uav = item as UAVFunction;
        if (uav)
        {
            uav.m_HP = m_UAVHP;
            uav.m_AmmoCapacity = m_AmmoCapacity;
            if(m_Target)
                uav.Target = m_Target.gameObject;
        }
        m_LoadingCount--;
        m_UAVSortileList.Add(uav);
        return item;
    }

    public override void FireOnlyEffect(GameObject ship, WeaponSlot slot, Vector3 position, Vector3 direction)
    {
    }

    public void ArmShip(Ship ship)
    {
        if (ship == null)
            return;
        m_Target = ship;
        foreach (var uav in m_UAVSortileList)
        {
            uav.Target = ship.gameObject;
        }
    }
    public override FlyItem Fire(GameObject ship, WeaponSlot slot, Vector3 position, Vector3 direction)
    {
        return null;
    }
    public override int update()
    {
        if(Slot && Slot.m_FireReady == true && 
            m_LoadingCount + m_UAVSortileList.Count < m_LoadingCapacity &&
            m_Ship)
        {
            m_LoadingCount++;
            Slot.m_FireReady = false;
            Slot.ShutCooling = 0;
        }
        m_OutTime += Time.deltaTime;
        return base.update();
    }
    public void SetUAVState(UAVFunction.State state)
    {
        foreach(var uavf in m_UAVSortileList)
        {
            uavf.m_State = state;
        }
    }

    public void SetSelfDestruct(bool value)
    {
        foreach (var uavf in m_UAVSortileList)
        {
            uavf.SelfDestruct = value;
        }
    }
    public void Recycle(UAVFunction uav)
    {
        RemoveUAV(uav);
        m_LoadingCount++;
        if(m_Ship)
            m_Ship.m_ShipStruct.Electric += m_BornSpend * 0.75f;
    }
    public void RemoveUAV(UAVFunction uav)
    {
        m_UAVSortileList.Remove(uav);
    }
}